package com.qut.graduate.busrtquery.utils.common;

import android.content.Context;
import android.content.res.AssetManager;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.qut.graduate.busrtquery.bean.BusLineInfo;
import com.qut.graduate.busrtquery.bean.BusInfoLineQuery;
import com.qut.graduate.busrtquery.bean.BusRealTimeInfo;
import com.qut.graduate.busrtquery.bean.BusStation;
import com.qut.graduate.busrtquery.greendao.bean.BusLine;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author WLL
 * @Time: 2019/4/27 14:59
 */
public class GsonUtils {
    private static final String TAG = "GsonUtils";
    private static GsonUtils mGsonUtils;
    private static Gson gson;

    public static GsonUtils getInstance() {
        if (null == mGsonUtils) {
            synchronized (GsonUtils.class) {
                if (null == mGsonUtils) {
                    mGsonUtils = new GsonUtils();
                }
            }
        }
        return mGsonUtils;
    }

    private GsonUtils() {
        gson = new Gson();
    }

    /**
     * 将object对象转成json字符串
     *
     * @param object
     * @return
     */
    public String BeanToString(Object object) {
        String gsonString = null;
        if (gson != null) {
            gsonString = gson.toJson(object);
        }
        return gsonString;
    }

    /**
     * 将gsonString转成泛型bean
     *
     * @param gsonString
     * @param cls
     * @return
     */
    public <T> T StringToBean(String gsonString, Class<T> cls) {
        T t = null;
        if (gson != null) {
            String myJson = gson.toJson(gsonString);
            t = gson.fromJson(myJson, cls);
        }
        return t;
    }

    /**
     * 转成list
     * 泛型在编译期类型被擦除导致报错
     *
     * @param gsonString
     * @param cls
     * @return
     */
    public <T> List<T> StringToList(String gsonString, Class<T> cls) {
        List<T> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
            }.getType());
        }
        return list;
    }

    /**
     * 转成list,解决泛型问题
     *
     * @param json
     * @param cls
     * @param <T>
     * @return
     */
    public <T> List<T> jsonToList(String json, Class<T> cls) {
        try {
            Gson gson = new Gson();
            List<T> list = new ArrayList<T>();
            JsonArray array = new JsonParser().parse(json).getAsJsonArray();
            for (final JsonElement elem : array) {
                list.add(gson.fromJson(elem, cls));
            }
            return list;
        } catch (Exception e) {
            LogUtils.d("----GsonUtils  E-json字符串转list错误" + e);
        }
        return null;
    }

    /**
     * 转成list中有map的
     *
     * @param gsonString
     * @return
     */
    public <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
        List<Map<String, T>> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString,
                    new TypeToken<List<Map<String, T>>>() {
                    }.getType());
        }
        return list;
    }

    /**
     * 转成map
     *
     * @param gsonString
     * @return
     */
    public <T> Map<String, T> GsonToMaps(String gsonString) {
        Map<String, T> map = null;
        if (gson != null) {
            map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
            }.getType());
        }
        return map;
    }

    /**
     * 将保存在assets文件夹下的指定名称的json文件读取到字符串中
     *
     * @param fileName
     * @param context
     * @return
     */
    public String getJson(String fileName, Context context) {
        // 将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            // 获取assets资源管理器
            AssetManager assetManager = context.getAssets();
            // 通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    public List<BusRealTimeInfo> getBusRealTimeInfo(String responseBusInfo){
        List<BusRealTimeInfo> busRealTimeInfoList = new ArrayList<>();

            BusRealTimeInfo busRealTimeInfo = new BusRealTimeInfo();

        return busRealTimeInfoList;
    }

    // 解析搜索公交界面点击时请求站点信息返回的json字符串
    public BusLineInfo getBusInfo(String responseBusInfo, BusInfoLineQuery busInfoLineQuery) {
        BusLineInfo busLineInfo = new BusLineInfo();
        try {
            JSONObject jsonObject = null;
            jsonObject = new JSONObject(responseBusInfo);
            JSONObject routes = jsonObject.getJSONObject("routes");
            JSONArray jsonArray = routes.getJSONArray(String.valueOf(busInfoLineQuery.getRid()));
            JSONObject jsonObject2 = (JSONObject) jsonArray.get(0);
            busLineInfo.setRouteid(jsonObject2.getString("routeid"));
            busLineInfo.setStation(jsonObject2.getString("station"));
            busLineInfo.setTime(jsonObject2.getString("time"));
            busLineInfo.setStation1(jsonObject2.getString("station1"));
            busLineInfo.setTime1(jsonObject2.getString("time1"));
            busLineInfo.setPrice(jsonObject2.getString("price"));
            busLineInfo.setLine(jsonObject2.getString("line"));
            busLineInfo.setLine1(jsonObject2.getString("line1"));
            busLineInfo.setLine2(jsonObject2.getString("line2"));
            busLineInfo.setRoutename(jsonObject2.getString("routename"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return busLineInfo;
    }

    // 解析搜索公交界面点击时请求站点信息返回的json字符串
    public BusLineInfo getBusInfo(String responseBusInfo, BusLine busLine) {
        BusLineInfo busLineInfo = new BusLineInfo();
        try {
            JSONObject jsonObject = null;
            jsonObject = new JSONObject(responseBusInfo);
            JSONObject routes = jsonObject.getJSONObject("routes");
            JSONArray jsonArray = routes.getJSONArray(String.valueOf(busLine.getRid()));
            JSONObject jsonObject2 = (JSONObject) jsonArray.get(0);
            busLineInfo.setRouteid(jsonObject2.getString("routeid"));
            busLineInfo.setStation(jsonObject2.getString("station"));
            busLineInfo.setTime(jsonObject2.getString("time"));
            busLineInfo.setStation1(jsonObject2.getString("station1"));
            busLineInfo.setTime1(jsonObject2.getString("time1"));
            busLineInfo.setPrice(jsonObject2.getString("price"));
            busLineInfo.setLine(jsonObject2.getString("line"));
            busLineInfo.setLine1(jsonObject2.getString("line1"));
            busLineInfo.setLine2(jsonObject2.getString("line2"));
            busLineInfo.setRoutename(jsonObject2.getString("routename"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return busLineInfo;
    }


    // 解析搜索公交界面点击时请求站点信息返回的json字符串
    public List<BusStation> getBusStation(String responseBusInfo, BusInfoLineQuery busInfoLineQuery) {
        List<BusStation> busStationList = new ArrayList<>();
        try {
            JSONObject jsonObject = null;
            jsonObject = new JSONObject(responseBusInfo);
            JSONObject stations = jsonObject.getJSONObject("stations");
            JSONArray jsonArray = stations.getJSONArray(String.valueOf(busInfoLineQuery.getRid()));
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject2 = (JSONObject) jsonArray.get(i);
                BusStation busStation = new BusStation();
                busStation.setRouteid(jsonObject2.getString("routeid"));
                busStation.setStationid(jsonObject2.getString("stationid"));
                busStation.setStationdualid(jsonObject2.getString("stationdualid"));
                busStation.setStationtype(jsonObject2.getString("stationtype"));
                busStation.setSegmentname(jsonObject2.getString("segmentname"));
                busStation.setSegmentid(jsonObject2.getString("segmentid"));
                busStation.setStaionname(jsonObject2.getString("staionname"));
                busStation.setGroupname(jsonObject2.getString("groupname"));
                busStation.setDirection(jsonObject2.getString("direction"));
                busStation.setSubwayname(jsonObject2.getString("subwayname"));
                busStationList.add(busStation);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return busStationList;
    }

    // 解析搜索公交界面点击时请求站点信息返回的json字符串
    public List<BusStation> getBusStation(String responseBusInfo, BusLine busLine) {
        List<BusStation> busStationList = new ArrayList<>();
        try {
            JSONObject jsonObject = null;
            jsonObject = new JSONObject(responseBusInfo);
            JSONObject stations = jsonObject.getJSONObject("stations");
            JSONArray jsonArray = stations.getJSONArray(String.valueOf(busLine.getRid()));
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject2 = (JSONObject) jsonArray.get(i);
                BusStation busStation = new BusStation();
                busStation.setRouteid(jsonObject2.getString("routeid"));
                busStation.setStationid(jsonObject2.getString("stationid"));
                busStation.setStationdualid(jsonObject2.getString("stationdualid"));
                busStation.setStationtype(jsonObject2.getString("stationtype"));
                busStation.setSegmentname(jsonObject2.getString("segmentname"));
                busStation.setSegmentid(jsonObject2.getString("segmentid"));
                busStation.setStaionname(jsonObject2.getString("staionname"));
                busStation.setGroupname(jsonObject2.getString("groupname"));
                busStation.setDirection(jsonObject2.getString("direction"));
                busStation.setSubwayname(jsonObject2.getString("subwayname"));
                busStationList.add(busStation);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return busStationList;
    }

}
